package com.dao;

import com.pojo.order;
import com.util.dbUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class orderDao {


    /**
     * 查询所有订单信息
     */
    public ResultSet queryAllOrder(int userid) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="select * from shopping.order where userid='"+userid+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet;
    }

    /**
     * 添加订单信息
     */
    public boolean add(order order) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="insert into shopping.order(orderid,userid,bookid,bookcount,price,buyTime) values ("+"'"+order.getOrderid()+"'," +order.getUserid()+","+ "'"+order.getBookid()+"',"+order.getBookcount()+","+order.getPrice()+","+"'"+order.getBuyTime()+"'"+  ")";
        int i = statement.executeUpdate(sql);
        if(i==-1){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 删除订单
     */
    public boolean deleteOrder(int userid,String orderid) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="delete from shopping.order where userid="+userid+" and orderid= '"+orderid+"'";
        int i = statement.executeUpdate(sql);
        if(i==-1){
            return false;
        }else {
            return true;
        }
    }


    /**
     * 查询单个订单
     */
    public ResultSet queryOneOrder(int userid,String orderid) throws SQLException, ClassNotFoundException {
        Statement statement = dbUtil.getStatement();
        String sql="select * from shopping.order where userid="+userid+" and orderid ='"+orderid+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet;
    }
    /**
     * 确认收货
     * @Receive 当receive =0 is 未收货 ，反之收货
     */
    public void  receiveSuccess(int userid,String orderid) throws SQLException, ClassNotFoundException {
        String sql="update shopping.order set receive="+1+" and orderid='"+orderid+"' and userid="+userid;
        Statement statement = dbUtil.getStatement();
         statement.executeUpdate(sql);
    }















}
